
package cn.javaxc.mvcwork.modules.login;
import cn.javaxc.mvcwork.controller.SystemStatus;
import cn.javaxc.mvcwork.controller.web.ClientType;
import cn.javaxc.mvcwork.framework.beans.TFormBean;
import cn.javaxc.mvcwork.framework.database.ConnectionElement;
import cn.javaxc.mvcwork.modules.admin.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.GrantedAuthority;

public class UserBean extends TFormBean implements UserDetails{
  private Object userId = -1;
  private Object actualUserId = -1;
  private String template = null;
  private String cssFile = "";
  private java.util.Date permissionCheck = new java.util.Date();
  private java.util.Date hierarchyCheck = new java.util.Date();
  private String idRange = "";
  private transient ClientType clientType = null;
  private ConnectionElement connectionElement = null;
  private String sessionId = null;

  //TODO: Remove these from the user's session because they can't be serialized
  private User userRecord = null;



  
  public UserBean() { }

  @JsonIgnore
  public Object getId(){
	return getUserId();
  }
  
  public void setUserId(Object tmp) {
    userId = tmp;
  }


  
  public void setIdRange(String tmp) {
    this.idRange = tmp;
  }


  
  public void setCssFile(String tmp) {
    this.cssFile = tmp;
  }


  
  public void setTemplate(String tmp) {
    this.template = tmp;
  }


  
  public void setPermissionCheck(java.util.Date tmp) {
    this.permissionCheck = tmp;
  }


  
  public void setHierarchyCheck(java.util.Date tmp) {
    this.hierarchyCheck = tmp;
  }


  
  public void setUserRecord(User tmp) {
    this.userRecord = tmp;
  }


  
  public void setClientType(ClientType tmp) {
    this.clientType = tmp;
  }

  
  public void setConnectionElement(ConnectionElement connectionElement) {
    this.connectionElement = connectionElement;
  }


  
  public void setActualUserId(Object actualUserId) {
    this.actualUserId = actualUserId;
  }


  
  public Object getActualUserId() {
    return actualUserId;
  }


  
  public SystemStatus getSystemStatus(ServletConfig config) {
    return (this.getSystemStatus(config.getServletContext()));
  }


  
  public SystemStatus getSystemStatus(ServletContext context) {
    Hashtable globalStatus = (Hashtable) context.getAttribute("SystemStatus");
    return (SystemStatus) globalStatus.get(connectionElement.getUrl());
  }


  
  public ConnectionElement getConnectionElement() {
    return connectionElement;
  }


  
  public User getUserRecord() {
    return userRecord;
  }


  @JsonIgnore  
  public String getUsername() {
    return userRecord.getUsername();
  }


  
  public Object getUserId() {
    return userId;
  }


  
//  public String getBrowserId() {
//    return clientType.getBrowserId();
//  }
//
//
//  
//  public double getBrowserVersion() {
//    return clientType.getBrowserVersion();
//  }


  @JsonIgnore  
  public int getRoleId() {
    return getUserRecord().getRoleId();
  }


  @JsonIgnore  
  public String getRole() {
    return getUserRecord().getRole();
  }


  @JsonIgnore  
  public int getRoleType() {
    return getUserRecord().getRoleType();
  }


  @JsonIgnore  
  public int getSiteId() {
    return getUserRecord().getSiteId();
  }


  @JsonIgnore  
  public int getOrgId() {
    return getUserRecord().getContact().getOrgId();
  }


  
  public String getCssFile() {
    return cssFile;
  }


  @JsonIgnore  
  public Contact getContact() {
    return getUserRecord().getContact();
  }


  @JsonIgnore  
  public String getContactName() {
    return getUserRecord().getContact().getNameFirstLast();
  }


  @JsonIgnore
  public Locale getLocale() {
    return getUserRecord().getLocale();
  }


  @JsonIgnore  
  public String getTimeZone() {
    return getUserRecord().getTimeZone();
  }


  @JsonIgnore  
  public String getCurrency() {
    return getUserRecord().getCurrency();
  }


  
  public String getTemplate() {
    return template;
  }


 

  
  public ClientType getClientType() {
    return clientType;
  }


  
  public java.util.Date getPermissionCheck() {
    return permissionCheck;
  }


  
  public java.util.Date getHierarchyCheck() {
    return hierarchyCheck;
  }

  public String getSessionId() {
    return sessionId;
  }

  public void setSessionId(String sessionId) {
    this.sessionId = sessionId;
  }
  public String toString(){
	return "userId:"+userId+",actualId:"+actualUserId+",userRecord:"+userRecord;
  }
    @JsonIgnore
    @Override
    public String getPassword()
    {
        return userRecord.getPassword();
    }


    /**
     * 账户是否未过期,过期无法验证
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired()
    {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     * 
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked()
    {
        return true;
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     * 
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired()
    {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     * 
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isEnabled()
    {
        return true;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities()
    {
        return null;
    }
}
